class submitFormView {
  _parentElement = document.querySelector(".form");
  _submitFormButton = document.querySelector(".input-submit__button");

  _noticeFormWindow = document.querySelector(".notice--submit");
  _noticeFormWindowIcon = document.querySelector(".notice__icon .iconfont");
  _noticeFormWindowMessage = document.querySelector(".notice__msg");

  renderError(error) {
    let iconClass = "icon-Happyface";
    switch (error.name) {
      case 1000:
        iconClass = "icon-Why";
        break;
      case 1001:
      case 1002:
        iconClass = "icon-What";
        break;
      case 1003:
      case 1004:
      case 1005:
        iconClass = "icon-Dead";
        break;
    }
    this._noticeFormWindowIcon.classList.add(iconClass);
    this._noticeFormWindowMessage.innerHTML = error.message;
    this._noticeFormWindow.classList.remove("hidden");
    setTimeout(() => {
      this._noticeFormWindow.classList.add("hidden");
    }, 2000);
  }

  getData(handler) {
    const submitForm = new FormData(this._parentElement);
    const rawForm = Object.fromEntries(submitForm);
    handler(rawForm);
  }

  clearForm() {
    const formInputs = this._parentElement.querySelectorAll("[name]");
    for (const inp of formInputs) {
      inp.value = "";
    }
  }

  dataEcho(form) {
    const formInputs = this._parentElement.querySelectorAll("[name]");
    for (const inp of formInputs) {
      const name = inp.name;
      inp.value = form[name];
    }
  }

  addHandlerSubmit(handler) {
    this._submitFormButton.addEventListener(
      "click",
      this.getData.bind(this, handler)
    );
  }
}

export default new submitFormView();
